//手机号码
const phoneRegex = (str) => {
  return /^[1][3,4,5,6,7,8,9][0-9]{9}$/.test(str);
}

//邮箱
const emailRegex = (str) => {
  return /^([A-Za-z0-9_\-\.])+\@([A-Za-z0-9_\-\.])+\.([A-Za-z]{2,4})$/.test(str);
}
// 给图片添加水印
export const addFileWatermark = async (file, text = new Date().toLocaleString()) => {
  return new Promise((resolve) => {
    const reader = new FileReader();
    reader.readAsDataURL(file); //file转base64
    reader.onload = (e) => {
      let canvas = document.querySelector("canvas");
      if (!canvas) {
        canvas = document.createElement("canvas");
      }
      const ctx = canvas.getContext("2d");
      if (ctx == null) return;
      const img = new Image();

      if (e.target == null) return;
      img.src = e.target.result;

      img.onload = () => {
        let imgWidth = img.width;
        let imgHeight = img.height;
        // let fontsize = imgWidth > imgHeight ? imgHeight / 20 : imgWidth / 20;//设定文字大小随图片大小变化
        canvas.width = imgWidth; //画布宽度
        canvas.height = imgHeight; //画布高度

        let imgurl = "";
        ctx.drawImage(img, 0, 0, imgWidth, imgHeight); //绘制图片大小和先前图片一致
        ctx.font = `${imgWidth / 20}px Verdana`; //文字大小
        ctx.shadowOffsetX = -1; //用来设定阴影在 X轴的延伸距
        ctx.shadowOffsetY = -1; //用来设定阴影在 Y轴的延伸距
        ctx.shadowBlur = 0; //设定阴影的模糊程度 默认0
        ctx.shadowColor = "rgba(0, 0, 0, 0.95)"; //设定阴影颜色效果
        ctx.fillStyle = "#ffffff"; //设置字体颜色
        ctx.textBaseline = "bottom";
        ctx.textAlign = "right";
        ctx.fillText(
          text,
          canvas.width - 10,
          canvas.height - 10
        );
        //修改文字的位置
        // imgurl = canvas.toDataURL(file.type, 0.8); //输出压缩后的base64
        imgurl = canvas.toDataURL("image/jpeg"); //输出压缩后的base64
        resolve({ url: imgurl, flag: true });
      };
    };
  });
};

//blob下载文件
export const downloadFile = (res, fileName = '文件.xlsx') => {
  return new Promise((resolve, reject) => {
    try {
      const blob = new Blob([res.data], {
        type: 'application/octet-stream; charset=utf-8'
      })
      const fileNames = res.headers['content-disposition']
      const a = document.createElement('a') // 创建一个a标签
      a.download = fileNames; // 指定文件名称
      a.download = decodeURIComponent(fileNames.match(/=(.*)$/)[1]) // 指定文件名称
      a.style.display = 'none' // 页面隐藏
      a.href = URL.createObjectURL(blob) // href用于下载地址
      document.body.appendChild(a) // 插到页面上
      a.click() // 通过点击触发
      URL.revokeObjectURL(a.href) // 释放URL 对象
      document.body.removeChild(a) // 删掉a标签
      resolve(true)
    } catch (error) {
      resolve(false)
    }
  })
}
// url下载
export const downloadUrlFile = (url) => {
  const a = document.createElement('a') // 创建一个a标签
  a.style.display = 'none' // 页面隐藏
  a.href = url // href用于下载地址
  document.body.appendChild(a) // 插到页面上
  a.click() // 通过点击触发
  document.body.removeChild(a) // 删掉a标签
}
//base64下载
export const dataURLToFile = (dataURL, filename) => {
  const arr = dataURL.split(',');
  const mime = arr[0].match(/:(.*?);/)[1]; //mime类型 image/png
  const bstr = atob(arr[1]); //base64 解码
  let n = bstr.length,
    u8arr = new Uint8Array(n);

  while (n--) {
    u8arr[n] = bstr.charCodeAt(n);
  }

  // return new File([u8arr], filename, { type: mime });
  let blob = new Blob([u8arr], { type: mime });
  const a = document.createElement('a') // 创建一个a标签
  a.download = filename; // 指定文件名称
  a.style.display = 'none' // 页面隐藏
  a.href = URL.createObjectURL(blob) // href用于下载地址
  document.body.appendChild(a) // 插到页面上
  a.click() // 通过点击触发
  URL.revokeObjectURL(a.href) // 释放URL 对象
  document.body.removeChild(a) // 删掉a标签
}
export function formatDateTime(date, format = 'YYYY-MM-DD HH:MM:SS') {
  function padZero(num) {
    return num < 10 ? '0' + num : num;
  }
  date = new Date(date);
  var year = date.getFullYear();
  var month = padZero(date.getMonth() + 1);
  var day = padZero(date.getDate());
  var hours = padZero(date.getHours());
  var minutes = padZero(date.getMinutes());
  var seconds = padZero(date.getSeconds());

  return format.replace("YYYY", year)
    .replace("MM", month)
    .replace("DD", day)
    .replace("HH", hours)
    .replace("MM", minutes)
    .replace("SS", seconds)
}